//
//  InsDetailViewController.m
//  grainHouse
//
//  Created by zzxcc on 14/10/19.
//  Copyright (c) 2014年 zzxcc. All rights reserved.
//

#import "InsDetailViewController.h"
#import "InsDetailTableViewCell.h"
#import "InsDetail1TableViewCell.h"
#import "InsDetail2TableViewCell.h"
#import "UIImageView+WebCache.h"
#import "InsData.h"
#import "InsModel.h"


@interface InsDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UIView *view1;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray  *array;
@property(nonatomic, assign)float height;

@end

@implementation InsDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        InsData *ins = [[InsData alloc]init];
        
        [ins getdata:^(NSMutableArray *arry, NSMutableArray *array, NSMutableArray *array1) {
            
            //NSLog(@"%@", [arry[0] title]);
            _array = array1;
            // NSLog(@"%@", array1[1]);
           /// NSLog(@"%ld", array1.count);
            [self.tableView reloadData];
        }];
        
        
        
        //[ins getdata];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 580)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [self addFooterView];
    
}

- (void)addFooterView
{
    _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 530, 320, 50)];
    _view1.backgroundColor = [UIColor blackColor];
    [self.tableView addSubview:_view1];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 100, 40);
   // leftButton.backgroundColor = [UIColor yellowColor];
    [leftButton setTitle:@"转发" forState:UIControlStateNormal];
    [_view1 addSubview:leftButton];
    
    UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    centerButton.frame = CGRectMake(110, 0, 100, 40);
   // centerButton.backgroundColor = [UIColor redColor];
    [centerButton setTitle:@"评论" forState:UIControlStateNormal];
    [_view1 addSubview:centerButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(220, 0, 100, 40);
   // rightButton.backgroundColor = [UIColor greenColor];
    [rightButton setTitle:@"喜欢" forState:UIControlStateNormal];
    [_view1 addSubview:rightButton];
    
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _view1.frame = CGRectMake(_view1.frame.origin.x, 530 + self.tableView.contentOffset.y , _view1.frame.size.width, _view1.frame.size.height);
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return  [_array[_hang ] count] + 2;
    }
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0){
            return 70;
        }
        if (indexPath.row == 1){
            return 70;
        }
        if(indexPath.row > 1 && indexPath.row < [_array[_hang]count] + 3){
//            CGSize requiredSize = [[_array[_hang][indexPath.row - 2] valueForKey:@"comment"] boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10]} context:nil].size;
//            NSLog(@"-----%f", requiredSize.height);
//            return requiredSize.height + 150;
            return _height;
        }
        
        
            
        }
        
    
    return 40;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 3;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
      if (indexPath.row == 0) {
    
        NSString *vc =[NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
    
        InsDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:vc];
    
        if (cell == nil) {
            cell = [[InsDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:vc];
            cell.titleLable.text = _titleTop;
        
        }
        return cell;
    }
    else if (indexPath.row == 1) {
        NSString *vc =[NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        
        InsDetail1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:vc];
        
        if (cell == nil) {
            cell = [[InsDetail1TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:vc];
            
        }
        cell.nameLabel.text = _nameLabel;
        [cell.userImage  sd_setImageWithURL:_faceUrl placeholderImage:nil options:SDWebImageRefreshCached];;
        NSLog(@"%@", _nameLabel);
        return cell;
    }
    else{
        NSString *vc =[NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:vc];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:vc];
            
        }
        //NSLog(@"%@", _array[_hang][indexPath.row - 2]);
        cell.textLabel.text = [_array[_hang][indexPath.row - 2] valueForKey:@"comment"];
       cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont systemFontOfSize:10];
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.font,NSFontAttributeName, nil];
      CGSize requiredSize = [[_array[_hang][indexPath.row - 2] valueForKey:@"comment"] boundingRectWithSize:CGSizeMake(300, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
        _height = requiredSize.height;
        
        CGRect rect = cell.frame;
       rect.size.height = requiredSize.height ;
        cell.frame = rect;

        return cell;
        
     }
    
    }
    NSString *vc =[NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:vc];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:vc];
        
    }
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
